﻿@using TwincatDashboard.Services
<MDataTable Headers="DataTableHeaders"
            ResizeMode="DataTableResizeMode.Auto"
            SelectedChanged="SelectedAdsRouteChanged"
            ShowSelect SingleSelect ItemKey="x=>x.Name"
            HideDefaultFooter Dense
            Items="AdsRouteInfo"/>

@code {
    [Parameter] public List<AdsRouteInfo> AdsRouteInfo { get; set; } = [];
    [Parameter] public EventCallback<AdsRouteInfo> SelectedAdsRoute { get; set; }

    private async Task SelectedAdsRouteChanged(IEnumerable<string> selected)
    {
        var selectedRouteList = selected.ToList();
        if (!selectedRouteList.Any()) selectedRouteList = ["Local"];
        var selectedAdsRoute = AdsRouteInfo.FirstOrDefault(
            x => x.Name == selectedRouteList.FirstOrDefault());
        Log.Information("SelectedAdsRoute: {RouteName}", selectedAdsRoute?.Name);
        await SelectedAdsRoute.InvokeAsync(selectedAdsRoute);
    }

    public readonly List<DataTableHeader<AdsRouteInfo>> DataTableHeaders =
    [
        new() { Text = "Name", Value = nameof(Services.AdsRouteInfo.Name) },
        new() { Text = "Address", Value = nameof(Services.AdsRouteInfo.Address) },
        new() { Text = "NetId", Value = nameof(Services.AdsRouteInfo.NetId) }
    ];

}